package com.xy.sql.xml.node.impl;

import com.xy.sql.xml.node.XySQLSuperNode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XySQLMapperNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "mapper";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLMapperNode();
    }

    public String getNamespace() {
        return this.getPropertyMap().get("namespace");
    }


    //模型
    private XySQLModelNode model;

    //模型属性
    private Map<String, Map<String, String>> itemMap;

    /**
     * 取得模型
     *
     * @return
     */
    public XySQLModelNode getModel() {
        if (model == null) {

            if (this.getChildList() == null) {
                return null;
            }
            for (XySQLSuperNode node : this.getChildList()) {
                if (node instanceof XySQLModelNode) {
                    model = (XySQLModelNode) node;
                }
            }
        }
        return model;
    }

    /**
     * 取得模型信息
     *
     * @return
     */
    public Map<String, Map<String, String>> getItemMap() {
        if (itemMap == null) {
            itemMap = new HashMap<>();
            if (getModel() != null) {
                List<XySQLSuperNode> itemList = getModel().getChildList();
                if (itemList != null) {
                    for (XySQLSuperNode item : itemList) {
                        itemMap.putAll(((XySQLModelItemNode) item).getItemMap());
                    }
                }
            }
        }
        return itemMap;
    }
}
